//
//  FSSpecialTopicHomePageViewController.m
//  fanshu
//
//  Created by 许海峰 on 16/4/30.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSSpecialTopicHomePageViewController.h"
#import "FSSpecialTopicHomePageHeaderView.h"
#import "FSSpecialTopicHomePageCell.h"
#import "AFNetworking.h"
#import "FSUserAddAttentionItem.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"

@interface FSSpecialTopicHomePageViewController()
@property (nonatomic,weak) UIView *sectionHeader;
@property (nonatomic,weak) UIButton *preHeaderBtn;
@property (nonatomic,weak) UIView *buttomView;
@property (nonatomic,strong) NSMutableArray *userItemArray;
/** 发送请求的管理者 */
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
/** 请求路径 */
NSString * const XMGRequestURL = @"http://api.budejie.com/api/api_open.php";

@implementation FSSpecialTopicHomePageViewController


- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

-(NSMutableArray *)userItemArray
{
    if (_userItemArray == nil) {
        _userItemArray = [NSMutableArray array];
    }
    return _userItemArray;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    
    //调整(iOS7以上)表格分隔线
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
    //调整(iOS8以上)表格分隔线
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    self.tableView.rowHeight = 150;
    
    [self loadWebData];
    self.tableView.tableHeaderView = [[FSSpecialTopicHomePageHeaderView alloc] init];
    
}
-(void)loadWebData {
    /*******************************/
    // 取消请求
    // 仅仅是取消请求, 不会关闭session
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    // 创建请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @29;
    
    // 发送请求
    [self.manager GET:XMGRequestURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        // 存储maxtime
        //        self.maxtime = responseObject[@"info"][@"maxtime"];
        
        // 字典数组 -> 模型数组
        self.userItemArray = [FSUserAddAttentionItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        // 刷新表格
        [self.tableView reloadData];
        
        // 结束刷新(恢复刷新控件的状态)
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 结束刷新(恢复刷新控件的状态)
        [self.tableView.mj_header endRefreshing];
        
        
        // 如果是因为取消任务来到failure这个block, 就直接返回, 不需要提醒错误信息
        if (error.code == NSURLErrorCancelled) return;
        // 请求失败的提醒
        [SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"];
        
    }];
    
    /*************************/

}
#pragma mark tableView代理和数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.userItemArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"STCell";
    FSSpecialTopicHomePageCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[FSSpecialTopicHomePageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    //iOS8以后cell分隔线边距调整
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    cell.model = self.userItemArray[indexPath.row];
    return cell;
}
//自定义HeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 40;
    } else {
        return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    header.backgroundColor = [UIColor whiteColor];
    NSArray *arr = @[@"最新收入", @"最新评论", @"热门", @"更多"];
    for (int i = 0; i < arr.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];

        CGFloat btnW = header.width / arr.count;
        CGFloat btnH = header.height;
        btn.frame = CGRectMake(i * btnW, 0, btnW, btnH);
        btn.tag = i;
        [header addSubview:btn];
        [btn addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    //标识线
    UIView *buttomView = [[UIView alloc] initWithFrame:CGRectMake(0, header.height -3, header.width / arr.count, 2.5)];
    buttomView.backgroundColor = [UIColor orangeColor];
    [header addSubview:buttomView];
    self.buttomView = buttomView;
    //分隔线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, header.height -0.5, header.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [header addSubview:lineView];
    
    self.sectionHeader = header;
    return header;
}
-(void)headerBtnClick:(UIButton *)btn {
    self.preHeaderBtn.selected = NO;
    btn.selected = YES;
    self.preHeaderBtn = btn;
    [UIView animateWithDuration:0.25 animations:^{
        self.buttomView.x = btn.width * btn.tag;
    }];
}
@end
